var ExpressionValueUtil = function () {
    var kv = new Object();
    kv.init = function (table,add,height) {
        this.table = table;
        this.height=height;
        add.click(function () {
            kv.add();
        });
        this.initTable();
    };
    kv.initTable = function () {
        this.table.bootstrapTable({
            editable: true,
            striped: true,
            columns: [
                {
                    "title": "表达式",
                    "field": "value"
                    ,
                    formatter: function (value, row, index) {
                        var input = "<input type=\"text\" name=\"value\" class=\"form-control\" onblur=\"changeData(" + index + ", this);\" value='" + value + "' />";
                        return input;
                    }
                }, {
                    "title": "操作",
                    "field": "",
                    "width": "8%",
                    formatter: function (value, row, index) {
                        var d = "deleteData(" + index + ",this)";
                        var deleteButton = '<input type="button" onclick="' + d + '" style="margin-left: 2px" class="btn btn-primary" value="删除" >';
                        return deleteButton;
                    }
                }
            ],
            height: this.height,
            onPostBody: function () {
                var header = $(".fixed-table-header table thead tr th");
                var body = $(".fixed-table-header table tbody tr th");
                var footer = $(".fixed-table-header table tr th");
                body.each(function () {
                    header.width((this).width());
                    footer.width((this).width());
                });
            }
        });
    };
    /**
     * 新增一行空数据
     * **/
    kv.add = function () {
        var obj = {};
        obj.value = "";
        var data = this.table.bootstrapTable('getData');
        var index = 0;
        if (data && data.length > 0) {
            index = data.length;
        }
        this.table.bootstrapTable('insertRow', {index: index, row: obj});
    };
    /**
     * 加载数据
     * **/
    kv.loadData =function (data) {
        var array =JSON.parse(data);
        for(var i=0;i<array.length;i++){
            this.table.bootstrapTable('insertRow', {index: i, row: array[i]});
        }
    };
    kv.removeAll =function () {
      this.table.bootstrapTable('load',[]);
    };
    return kv;
};
deleteData = function (index,obj) {
    var parents = $(obj).parents();
    var table;
    for(var i=0;i<parents.length;i++){
        var node =  $(parents[i]);
        var tagName=$(node).prop('tagName');
        if(tagName=="TABLE"){
            table=node;
            break;
        }
    }
    table.bootstrapTable('removeByIndex', index)
};
changeData =function (index, obj) {
    var parents = $(obj).parents();
    var table;
    for(var i=0;i<parents.length;i++){
        var node =  $(parents[i]);
        var tagName=$(node).prop('tagName');
        if(tagName=="TABLE"){
            table=node;
            break;
        }
    }
    var value = $(obj).val();
    var name = $(obj).attr('name');
    var row = {};
    row[name] = value;
    table.bootstrapTable('updateRow', {index: index, row: row});
};
function switchExpression(value) {
    if(value ==4||value==5){
        $("#expressionPanel").show();
        $("#addExpression").show();
        $("#testExpression").show();
    }else {
        $("#expressionPanel").hide();
        $("#addExpression").hide();
        $("#testExpression").hide();
    }
}
function expressionCheck(expression,expectResult) {
    var url="/testCase/expressionCheck";
    var assertType =$("#assertType").val();
    $.ajax({
        url:url,
        method:"post",
        data:{
            assertType:assertType,
            expectResult:expectResult,
            expression:expression
        },success:function (result) {
            if(result.success){
                var d =result.data;
                Ewin.alert("当前的表达式正确:获取到的值分别是:"+d);
            }else {
                Ewin.alert("当前的表达式异常:"+result.msg);
            }
        }
    })
}